package com.qfedu.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidUtil {
    public static DataSource dataSource;
    //对数据源进行初始化
    static {
        try {
            Properties properties = new Properties();
            //通过类加载器获取配置文件的输入流
            InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties");
            //加载配置文件
            properties.load(is);
            //初始化数据源
            dataSource = DruidDataSourceFactory.createDataSource(properties);//会自动读取druid配置文件，创建出数据源（连接池）里面会自动创建连接对象

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接对象
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        }catch (Exception e){

        }
        return connection;
    }

    public static DataSource getDataSource() {
        return dataSource;
    }
}
